<?php

namespace App\Http\Controllers\admin;

use App\Http\Requests\TypeRequest;
use App\Model\ShopGoodModel;
use App\Model\AdminLogsModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\ShopTypeModel;
use Validator;

//主页
class TypeController extends Controller
{
	// 用户列表
	public function index(Request $request)
	{
		//搜索条件
		$keywords = $request->input('keywords');//关键字
		$pid = $request->input('pid');
		$pageParam['keywords'] = $keywords;
		$pageParam['pid'] = $pid;

		//类别(搜索)
		$sql = "SELECT * FROM `shop_type` ORDER BY concat(path,tid),sort desc";
		$all_list = DB::select($sql);

		//类别列表(展示)
		$order = "SELECT * FROM `shop_type` ORDER BY concat(path,tid),sort desc";
		$list = DB::table(DB::raw("($order) as res"))->where(function ($query) {
			if (request()->keywords) {
				$query->where('name','like',"%".request()->keywords."%");
			}
			if(request()->pid){
				$query->where('tid',request()->pid)
					  ->Orwhere('path','like',"%".request()->pid."%");
			}
		})->paginate(15);
		foreach($list->items() as $k=>$v){
			$v->exit = 2;
			if(ShopTypeModel::where('path','like',"%,".$v->tid.",%")->first()){
				$v->exit = 1;
			}
		}
		//分页
		$page = $list->appends($pageParam)->links();

		return view('admin.type.index')
			->with('keywords',$keywords)
			->with('pid',$pid)

			->with('pid',$pid)
			->with('session_id',session('admin_id'))
			->with('page',$page)
			->with('all_list',$all_list)
			->with('list',$list);
	}

	//添加页
	public function add()
	{
		return view('admin.type.add');
	}

	//添加
	public function insert(Request $request){
		$post = $request->post();
		$validator = Validator::make($post,[
			'name'=>'required|unique:shop_type,name',
			'sort'=>'required|digits_between:1,999',
			'picname' => 'required',
		],[
			'name.required' => '分类不能为空',
			'name.unique' => '该分类已存在',
			'sort.required' => '请输入排序',
			'sort.digits_between' => '排序应为 1到999 之间',
			'picname.required' => 'Logo不能为空',
		]);
		if($validator->fails()){
			return returnJson(0, $validator->errors()->first());
		}
		//事物
		DB::beginTransaction();
		try {
			$type = New ShopTypeModel();
			if(ShopTypeModel::where('name',request()->name)->first()){
				return ['code'=>0,'msg'=>'该类别名称已存在'];
			}
			if(request()->pid){
				$ptype = ShopTypeModel::where('id',request()->pid)->first();
				if($ptype){
					if(ShopTypeModel::where('name',request()->name)->where('pid',request()->pid)->first()){
						return ['code'=>0,'msg'=>'该类别名称已存在'];
					}
					$type->pid = $ptype->id;
					$type->path = $ptype->path.$ptype->id.",";
					$type->level = $ptype->level+1;
				}else{
					return returnJson(0, '该父类不存在');
				}
			}

			$type->name = request()->name;
			$type->picname = request()->picname;
			$type->sort = request()->sort;
			$type->save();
			DB::commit();
		} catch (Exception $e) {
			DB::rollBack();
			throw $e;
			return returnJson(0,'操作失败,请稍后再试');
		}
		return returnJson(1,'添加成功');
	}

	//添加子类
	public function add_son(Request $request){
		$pid = $request->id;
		$type = ShopTypeModel::where('id',$pid)->first();
		if(!$type){
			return returnJson(0,'该父类不存在');
		}
		return view('admin.type.add_son',compact('type'));
	}

	//更改类别
	public function edit(Request $request){
		$id = request()->id;
		$type = ShopTypeModel::where('id',$id)->first();
		if($type->pid == 0){
			$fname = '无';
		}else{
			$fname = ShopTypeModel::where('id',$type->pid)->value('name');
		}
		return view('admin.type.edit',compact('type','fname'));
	}

	//编辑类别
	public function update(Request $request){
		$post = $request->post();
		$validator = Validator::make($post,[
			'name'=>'required',
			'sort'=>'required|digits_between:1,999',
//			'picname' => 'required',
		],[
			'name.required' => '分类不能为空',
			'sort.required' => '请输入排序',
			'sort.digits_between' => '排序应为 1到999 之间',
//			'picname.required' => 'Logo不能为空',
		]);
		if($validator->fails()){
			return returnJson(0, $validator->errors()->first());
		}
		if(ShopTypeModel::where('name',request()->name)->where('pid',0)->where('id','<>',request()->id)->first()){
			return ['code'=>0,'msg'=>'该类别名称已存在'];
		}
		if (ShopTypeModel::where('id', $post['id'])->update(["name" => request()->name,"picname"=>request()->picname,"sort"=>request()->sort])) {
			$msg = '编辑商品类别('.request()->name.')';
			AdminLogsModel::addLog($msg);
			return returnJson(1, '更改成功');
		} else {
			return returnJson(0, '更改失败');
		}
	}

	//删除
	public function del(){
		$id = request()->id;
		if(ShopGoodModel::where('tid',$id)->first()){
			return returnJson(0, '该分类下存在商品,不可删除');
		}else{
			if(ShopTypeModel::where('id',$id)->delete()){
				return returnJson(1, '删除成功');
			}else{
				return returnJson(0, '删除失败');
			}
		}

	}



}
